#!/bin/bash  -e

function info() {
echo Usage: `basename $0` '[-l lines_per_file] in.fastq(a)'
exit 65
}

while getopts  ":l:p:f:" opts
do
        case  $opts  in
        l) lines=$OPTARG;;
		p) out_prefix=$OPTARG;;
		f) suffix=$OPTARG;;
		*) info;;
        esac
done
shift $(($OPTIND - 1))


if [ $# -lt 1 ]; then info; fi

. /mnt/ilustre/app/medical/tools/.var


if test -z $suffix; then suffix=fastq; fi

# a number must be 4X
if test -z $lines; then lines=4000000; fi
echo $lines; 

prefix_len=7


split -a$prefix_len -d -l$lines $1 $out_prefix. || split -a$prefix_len -d -l$lines - $out_prefix.


# for file in $(ls $out_prefix.*); do
	# rename $file $file.$suffix $file
# done


for fq in $(ls $out_prefix.0*); do
	fq_index=$((${fq_index:=0}+1))
	ln -sf $fq $out_prefix.$fq_index.$suffix
done

. $cmd_done

